﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Threading;

namespace From {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            init();
        }//constructor

        private void button1_Click(object sender, EventArgs e) {
            BinaryFormatter inFormatter = new BinaryFormatter();
            inFormatter.Serialize(ns, pictureBox1.Image);
           
        }
        Socket sender_thd = new Socket(AddressFamily.InterNetwork,
                                           SocketType.Stream, ProtocolType.Tcp);
        NetworkStream ns;
        public void init() {

            // data buffer for incoming data 
            byte[] bytes = new byte[1024];

            // connect to a Remote device 
            try {
                // Establish the remote end point for the socket 
                IPHostEntry ipHost = Dns.GetHostEntry("192.168.1.2");
                IPAddress[] ipAddrs = new IPAddress[ipHost.AddressList.Length];
                // use only IPv4
                int z = 0;
                for (int i = 0; i < ipHost.AddressList.Length; i++) {
                    if (ipHost.AddressList[i].AddressFamily == AddressFamily.InterNetwork) {
                        ipAddrs[z++] = ipHost.AddressList[i];
                    }
                }
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddrs[0], 11000);
                // Connect the socket to the remote endpoint 

                sender_thd.Connect(ipEndPoint);
                Thread.Sleep(10);
                ns = new NetworkStream(sender_thd);
                //Stream stm = new MemoryStream();
                //pictureBox1.Image.Save(stm, System.Drawing.Imaging.ImageFormat.Jpeg);
}
            catch (Exception e) {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
                pictureBox1.Image = new Bitmap(ofd.FileName);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sender_thd.Shutdown(SocketShutdown.Both);
            sender_thd.Close();
        }
    }//class
}//namespace
